package com.happyscan138.makefun;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.happyscan138.makefun.BaseAdapter.Comment_item;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.View.ListViewForScrollView;
import com.happyscan138.makefun.View.RoundImageView;
import com.happyscan138.makefun.adapter.CommentAdapter;
import com.happyscan138.makefun.base.VideoPlayView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

import static com.happyscan138.makefun.Land.api;
import static com.happyscan138.makefun.Urls.Urls.Share;
import static com.happyscan138.makefun.Urls.Urls.base;
import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2016/12/1.
 */

public class questionDetailed extends AppCompatActivity implements View.OnClickListener, VideoPlayView.MediaPlayerImpl {

    private boolean isPlaying=false;
    private Button CommitAnswer;
    private LinearLayout linearLayoutA,linearLayoutB,linearLayoutC,linearLayoutD;
    private boolean isCheck=false;
    public String problem_id;
    private SharedPreferences preferences;
    public String access_key;
    private RoundImageView Questionhead;
    private TextView nick_Name,Questiontime,Questiontext;
    private TextView ReadCount,ReplayCount,CommentCount,AdmireCount;
    private TextView answer_A_text,answer_B_text,answer_C_text,answer_D_text;
    private String answerID_A,answerID_B,answerID_C,answerID_D;
    private RequestParams params1;
    private RequestParams params2;
    private String answerID;
    private TextView textViewComment,shouCang;
    public  LinearLayout CommentLinearalayout;
    private EditText editTextComment;
    public String comment;
    public Button btnSendComment ;
    private List<Comment_item> list;
    private ListViewForScrollView listViewcomment;
    private Comment_item data;
    private TextView Good;
    private String  sp_type,sp_typeA,sp_typeB,sp_typeC,sp_typeD;
    private CommentAdapter adapter;
    public int tag;
    private int atUserId=-1;
    public int positionUserId;
    private String like_type_id;
    private TextView share;
    private String url,WXtitle,WXdescription;
    private PopupWindow pop;
    private LayoutInflater inflater;
    private LinearLayout layout;
    private MediaPlayer mediaPlayer;
    private  int  CommentNum;
    private   String Thumb;
    private int admire;
    private int reply;
    private String m3u8;
    private String UserID;

    public questionDetailed() {
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question_detailed);
        preferences = getSharedPreferences("UserInfo", MODE_PRIVATE);
        access_key=preferences.getString("UserId","");
        like_type_id=preferences.getString("LikeTypeID","");
        UserID=preferences.getString("persionalID","");
        client=new AsyncHttpClient();
        params=new RequestParams();
        Intent intent=getIntent();
        problem_id=intent.getStringExtra("problem_id");
        inflater = LayoutInflater.from(this);
        initView();
        getNetData();
        getCommentNetData();
    }
    public void initView(){
        TextChange change=new TextChange();
        textViewComment=(TextView)findViewById(R.id.comment);
        textViewComment.setOnClickListener(this);
        Questionhead=(RoundImageView)findViewById(R.id.QuestionDigital_head);
        nick_Name=(TextView)findViewById(R.id.QuestionDigital_name);
        Questiontime=(TextView)findViewById(R.id.QuestionDigital_time);
        Questiontext=(TextView)findViewById(R.id.Question_text);
        ReadCount=(TextView)findViewById(R.id.Question_read);
        ReplayCount=(TextView)findViewById(R.id.Question_replay);
        CommentCount=(TextView)findViewById(R.id.Question_comment);
        AdmireCount=(TextView)findViewById(R.id.Admire_count);
        answer_A_text=(TextView)findViewById(R.id.textA);
        answer_B_text=(TextView)findViewById(R.id.textB);
        answer_C_text=(TextView)findViewById(R.id.textC);
        answer_D_text=(TextView)findViewById(R.id.textD);
        linearLayoutA=(LinearLayout)findViewById(R.id.answer_a);
        linearLayoutB=(LinearLayout)findViewById(R.id.answer_b);
        linearLayoutC=(LinearLayout)findViewById(R.id.answer_c);
        linearLayoutD=(LinearLayout)findViewById(R.id.answer_d);

        linearLayoutA.setOnClickListener(this);
        linearLayoutB.setOnClickListener(this);
        linearLayoutC.setOnClickListener(this);
        linearLayoutD.setOnClickListener(this);
        CommentLinearalayout=(LinearLayout)findViewById(R.id.input_comment) ;
        CommitAnswer=(Button)findViewById(R.id.commit_Answer);
        CommitAnswer.setOnClickListener(this);
        editTextComment=(EditText)findViewById(R.id.input);
        editTextComment.addTextChangedListener(change);
        btnSendComment=(Button)findViewById(R.id.send_comment);
        btnSendComment.setOnClickListener(this);
        listViewcomment=(ListViewForScrollView)findViewById(R.id.listView_inputComment);
        Good=(TextView)findViewById(R.id.dianzan);
        Good.setOnClickListener(this);
        share=(TextView)findViewById(R.id.share);
        share.setOnClickListener(this);
        shouCang=(TextView)findViewById(R.id.shoucang);
        shouCang.setOnClickListener(this);
    }
    public void getNetData(){
          params.put("access_key",access_key);
          params.put("problem_id",problem_id);
        RequestHandle handle=client.post(Urls.AnswerList,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                String jsonStr=response.toString();
                log.e("jsonStr",jsonStr);
                try {
                    JSONObject jsonObject=new JSONObject(jsonStr);
                    JSONObject data=jsonObject.getJSONObject("data");
                    String head=base+data.getString("img_path");
                    String nickName=data.getString("nick_name");
                    String time=data.getString("create_date");
                    String text=data.getString("content");
                    String MyselfId=data.getString("user_id");
                    final String resource=base+data.getString("resource");
                    Thumb=base+data.getString("thumb");
                    m3u8=base+data.getString("m3u8_file");
                    String read=data.getString("read_count");
                    reply=data.getInt("replay_count");
                    CommentNum=data.getInt("comment_count");
                    admire=data.getInt("comment_count");

                    Picasso.with(questionDetailed.this).load(head).into(Questionhead);
                    nick_Name.setText(nickName);
                    Questiontime.setText(time);
                    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.questiondetailed_question);
                    RelativeLayout relativeLayout=(RelativeLayout)linearLayout.findViewById(R.id.questionDetailed);
                    choose(resource,linearLayout,relativeLayout,m3u8);
                    Questiontext.setText(text);
                    ReadCount.setText(read);
                    ReplayCount.setText(reply+"");
                    CommentCount.setText(CommentNum+"");
                    AdmireCount.setText(admire+"");
                    JSONArray jsonArray=data.getJSONArray("answerList");
                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject tempA=jsonArray.getJSONObject(0);
                        answerID_A=tempA.getString("answer_id");
                        String answerA=tempA.getString("answer");
                        String answerA_resource=base+tempA.getString("an_resource");
                        log.e("UserID",UserID);
                        log.e("MyselfId",MyselfId);
                        if(UserID.equals(MyselfId)) {
                            sp_typeA = base+tempA.getString("sp_type");
                        }else {
                            sp_typeA=tempA.getString("sp_type");
                        }

                        answer_A_text.setText(answerA);
                        String m3u8A=base+tempA.getString("an_m3u8_file");
                        LinearLayout linearLayoutA = (LinearLayout) findViewById(R.id.questiondetailed_AnswerA);
                        RelativeLayout relativeLayoutA=(RelativeLayout)linearLayoutA.findViewById(R.id.detailedA);
                        choose(answerA_resource,linearLayoutA,relativeLayoutA,m3u8A);


                        JSONObject tempB=jsonArray.getJSONObject(1);
                        String answerB=tempB.getString("answer");
                        String answerB_resource=base+tempB.getString("an_resource");
                        log.e("UserID",UserID);
                        log.e("MyselfId",MyselfId);
                        if(UserID.equals(MyselfId)){
                            sp_typeB=base+tempB.getString("sp_type");
                        }else {
                            sp_typeB=tempB.getString("sp_type");
                        }
                        log.e("textSpecialB",sp_typeB);
                        answerID_B=tempB.getString("answer_id");
                        answer_B_text.setText(answerB);
                        log.e("answerB",answerB);
                        String m3u8B=base+tempB.getString("an_m3u8_file");
                        LinearLayout linearLayoutB = (LinearLayout) findViewById(R.id.questiondetailed_AnswerB);
                        RelativeLayout relativeLayoutB=(RelativeLayout)linearLayoutB.findViewById(R.id.detailedB);
                        choose(answerB_resource,linearLayoutB,relativeLayoutB,m3u8B);
                        JSONObject tempC=jsonArray.getJSONObject(2);
                        String answerC=tempC.getString("answer");
                        String answerC_resource=base+tempC.getString("an_resource");
                        if(UserID.equals(MyselfId)){
                            sp_typeC=base+tempC.getString("sp_type");
                        }else {
                            sp_typeC=tempC.getString("sp_type");
                        }
                        log.e("textSpecialC",sp_typeC);
                        answerID_C=tempC.getString("answer_id");
                        String m3u8C=base+tempC.getString("an_m3u8_file");
                        answer_C_text.setText(answerC);
                        LinearLayout linearLayoutC = (LinearLayout) findViewById(R.id.questiondetailed_AnswerC);
                        RelativeLayout relativeLayoutC=(RelativeLayout)linearLayoutC.findViewById(R.id.detailedC);
                        choose(answerC_resource,linearLayoutC,relativeLayoutC,m3u8C);

                        JSONObject tempD=jsonArray.getJSONObject(3);
                        String answerD=tempD.getString("answer");
                        String answerD_resource=base+tempD.getString("an_resource");
                        log.e("SaokuResourceaaaaaD",answerD_resource);
                        String m3u8D=base+tempD.getString("an_m3u8_file");
                        if(UserID.equals(MyselfId)){
                            sp_typeD=base+tempD.getString("sp_type");
                        }else {
                            sp_typeD=tempD.getString("sp_type");
                        }
                        LinearLayout linearLayoutD = (LinearLayout) findViewById(R.id.questiondetailed_AnswerD);
                        RelativeLayout relativeLayoutD=(RelativeLayout)linearLayoutD.findViewById(R.id.detailedD);
                        choose(answerD_resource,linearLayoutD,relativeLayoutD,m3u8D);
                        answerID_D=tempD.getString("answer_id");
                        answer_D_text.setText(answerD);

                        break;
                    };

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                log.e("onFailure",responseString);
            }
        });
    }
    public void getCommentNetData(){
        params2=new RequestParams();
        params2.put("type_key_val",problem_id);
        params2.put("page_count","10");
        params2.put("com_id_index","0");
        params2.put("type_id","1");
        log.e("paramscomment",params2.toString());
        RequestHandle handle=client.post(Urls.CommentList,params2,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                log.e("getCommentNetDataonSuccess",response.toString());
                list=new ArrayList<Comment_item>();
                String jsonStr=response.toString();
                try {
                    JSONObject  jsonObject=new JSONObject(jsonStr);
                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject objectData=jsonArray.getJSONObject(i);
                        data=new Comment_item();
                        data.head=base+objectData.getString("img_path");
                        data.name=objectData.getString("nick_name");
                        data.time=objectData.getString("com_date");
                        data.text=objectData.getString("comment");
                        data.User_ID=objectData.getString("user_id");
                        list.add(data);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                log.e("listsize",list.size()+"");
                adapter=new CommentAdapter(questionDetailed.this,list);
                adapter.setOnItemAddClick(new CommentAdapter.OnAddClickListener() {
                    @Override
                    public void onItemClick(int positionID, int adatag) {
                        tag=adatag;
                        positionUserId=positionID;
                        log.e("adapterTag",tag+"");
                        log.e("positionUserId",positionUserId+"");

                    }
                });
                adapter.notifyDataSetChanged();
                listViewcomment.setAdapter(adapter);

            }


            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                log.e("getCommentNetDataonFailure",responseString);
            }
        });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.answer_a:
                if(isCheck=!isCheck) {
                    log.e("isCheck","isCheck");
                    answerID=answerID_A;
                    sp_type=sp_typeA;
                    log.e("answerID",answerID);
                    linearLayoutA.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_selected));
                    linearLayoutB.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_offonclick));
                    linearLayoutC.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_offonclick));
                    linearLayoutD.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_offonclick));
                }
                if(isCheck==false){
                    linearLayoutA.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_offonclick));
               }
                break;
            case R.id.answer_b:
                if(isCheck=!isCheck) {
                    answerID=answerID_B;
                    sp_type=sp_typeB;
                    linearLayoutA.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_offonclick));
                    linearLayoutB.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_selected));
                    linearLayoutC.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_offonclick));
                    linearLayoutD.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_offonclick));
                }
                if(isCheck==false){
                    linearLayoutB.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_offonclick));
                }
                break;
            case R.id.answer_c:
                if(isCheck=!isCheck) {
                    answerID=answerID_C;
                    sp_type=sp_typeC;
                    linearLayoutA.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_offonclick));
                    linearLayoutB.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_offonclick));
                    linearLayoutC.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_selected));
                    linearLayoutD.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_offonclick));
                }
                if(isCheck==false){
                    linearLayoutC.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_offonclick));
                }
                break;
            case R.id.answer_d:
                if(isCheck=!isCheck) {
                    answerID=answerID_D;
                    sp_type=sp_typeD;
                    linearLayoutA.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_offonclick));
                    linearLayoutB.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_offonclick));
                    linearLayoutC.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_offonclick));
                    linearLayoutD.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_selected));
                }
                if(isCheck==false){
                    linearLayoutD.setBackgroundDrawable(getResources().getDrawable(R.drawable.linearlayout_offonclick));
                }
                break;
            case R.id.commit_Answer:
                params1=new RequestParams();
                log.e("commit_Answer","commit_Answer");
                params1.put("access_key",access_key);
                params1.put("problem_id",problem_id);
                if(answerID==null){
                    Toast.makeText(questionDetailed.this,"请选择答案",Toast.LENGTH_SHORT).show();
                }
                params1.put("answer_id",answerID);
                log.e("commit_Answer",params1.toString());
                client.post(Urls.CommitAnswer,params1,new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        String jsonStr=response.toString();
                        log.e("onSuccess",jsonStr);
                        try {
                            JSONObject jsonObject=new JSONObject(jsonStr);
                            String msg=jsonObject.getString("msg");
                            int a=jsonObject.getInt("code");

                            if(a==1){
                                ReplayCount.setText((reply+1)+"");
                                Toast.makeText(questionDetailed.this,msg,Toast.LENGTH_SHORT).show();
                                saokuResource(sp_type);
                                log.e("onSuccess",sp_type);
                            }else if(a==153){
                                Toast.makeText(questionDetailed.this,msg,Toast.LENGTH_SHORT).show();
                                return;
                            }
                            log.e("dsddddddddA",sp_type);



                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                        super.onFailure(statusCode, headers, responseString, throwable);
                        log.e("onFailure",responseString);
                    }
                });


//                if(!sp_typeB.equals(null)) {
//                    Bundle bundle = new Bundle();
//                    Intent intent=new Intent();
//                    bundle.putString("special",sp_typeB);
//                    intent.putExtras(bundle);
//                    intent.setClass(questionDetailed.this,viewSpecial.class);
//                    startActivity(intent);
//                }
//                if(!sp_typeC.equals(null)) {
//                    Bundle bundle = new Bundle();
//                    Intent intent=new Intent();
//                    bundle.putString("special",sp_typeC);
//                    intent.putExtras(bundle);
//                    intent.setClass(questionDetailed.this,viewSpecial.class);
//                    startActivity(intent);
//                }
//                log.e("dsdddddddd",sp_typeD);
//                if(!sp_typeD.equals(null)) {
//                    Bundle bundle = new Bundle();
//                    Intent intent=new Intent();
//                    bundle.putString("special",sp_typeD);
//                    intent.putExtras(bundle);
//                    intent.setClass(questionDetailed.this,viewSpecial.class);
//                    startActivity(intent);
//                }
                break;
            case R.id.comment:
                log.e("comment","comment");
                tag=1;
                log.e("tag",tag+"");
                CommentLinearalayout.setVisibility(View.VISIBLE);

                break;
            //发送评论
            case R.id.send_comment:

                if(tag==1) {
                    atUserId=0;
                }else if(tag==2){
                    atUserId=positionUserId;
                    log.e("positionUserID",atUserId+"");
                }
                    params = new RequestParams();
                    params.put("access_key", access_key);
                    params.put("comment", comment);
                    params.put("type_key_val", problem_id);
                    params.put("at_user_id",atUserId+"" );
                    params.put("com_id", "0");
                    params.put("type_id", "1");
                    log.e("at_user_id", atUserId+"");
                    RequestHandle handle1 = client.post(Urls.Comment, params, new JsonHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            super.onSuccess(statusCode, headers, response);
                            String jsonStr = response.toString();
                            try {
                                JSONObject data = new JSONObject(jsonStr);
                                String code = data.getString("code");
                                int a = Integer.valueOf(code).intValue();
                                if (a == 1) {
                                    CommentLinearalayout.setVisibility(View.GONE);
                                    getCommentNetData();
                                   // getNetData();

                                    CommentCount.setText((++CommentNum)+"");
                                    log.e("CommentNum",CommentNum+"");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    editTextComment.setText(null);


                break;
            case R.id.dianzan:
                RequestParams params3=new RequestParams();
                params3.put("access_key",access_key);
                params3.put("item_id",problem_id);

                log.e("params3",params3.toString());
                RequestHandle handle2=client.post(Urls.Dianzan,params3,new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        String jsonStr=response.toString();
                        try {
                            JSONObject jsonObject=new JSONObject(jsonStr);
                            String msg=jsonObject.getString("msg");
                            Toast.makeText(questionDetailed.this,msg,Toast.LENGTH_SHORT).show();
                            String code = jsonObject.getString("code");
                            int a = Integer.valueOf(code).intValue();
                            if(a==1){
                                AdmireCount.setText((admire+1)+"");
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                        super.onFailure(statusCode, headers, responseString, throwable);
                    }
                });

                break;
            case R.id.share:
                    // 引入窗口配置文件
                    View viewpop = inflater.inflate(R.layout.share, null);
                    pop = new PopupWindow(viewpop, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, false);

                    pop.setTouchable(true);
                    pop.setOutsideTouchable(true);
                    pop.setFocusable(true);
                    pop.update();
                    pop.setBackgroundDrawable(new BitmapDrawable());
                    Button friendnet = (Button) viewpop.findViewById(R.id.friendnet);
                    friendnet.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            shareArticleToWx(1);

                        }
                    });
                    Button friend = (Button) viewpop.findViewById(R.id.friend);
                    friend.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            shareArticleToWx(0);

                        }
                    });
                    pop.showAsDropDown(viewpop);
                break;
            case R.id.shoucang:
                RequestParams params4 =new RequestParams();
                params4.put("access_key",access_key);
                params4.put("problem_id",problem_id);
                params4.put("like_type_id",like_type_id);
                log.e("paramsss4",params4.toString());
                RequestHandle handle3=client.post(Urls.Like,params4,new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        String json=response.toString();
                        log.e("jsonsaaaaas",json);
                        try {
                            JSONObject jsonObject=new JSONObject(json);
                            String msg=jsonObject.getString("msg");
                            Toast.makeText(questionDetailed.this,msg,Toast.LENGTH_SHORT).show();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

                        super.onFailure(statusCode, headers, responseString, throwable);
                        log.e("jsonsaaaaas",responseString);
                    }
                });

                break;
        }
    }
    public void  choose(final String Res, LinearLayout lay, RelativeLayout Rel,String m3u8){
            log.e("resource",Res);
        if(Res.endsWith(".jpg")||Res.endsWith(".png")){
            layout = (LinearLayout) inflater.inflate(R.layout.saoku_image_item, null);
            ImageView imageView=(ImageView) layout.findViewById(R.id.saoku_image_item);
            Picasso.with(questionDetailed.this).load(Res).into(imageView);
            lay.removeView(Rel);
            lay.addView(layout);
        }else if(Res.endsWith(".mp4")){
            layout = (LinearLayout) inflater.inflate(R.layout.saoku_video_item, null);
            ImageView Img=(ImageView)layout.findViewById(R.id.detail_img);
            if(Thumb.equals(Urls.base)){
                Img.setImageDrawable(getResources().getDrawable(R.mipmap.makefun));
            }else {
                Picasso.with(questionDetailed.this).load(Thumb).into(Img);
            }
            replaceVieo(Res,layout,Thumb);
            lay.removeView(Rel);
            lay.addView(layout);
        }else if(Res.endsWith(".mp3")){
            layout = (LinearLayout) inflater.inflate(R.layout.dynamic_voice_b, null);
            ImageButton QuestionPlayB=(ImageButton)layout.findViewById(R.id.Dymanic_play_voice_B);
            QuestionPlayB.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mediaPlayer=new MediaPlayer();
                    try {
                        mediaPlayer.setDataSource(Res);
                        mediaPlayer.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mediaPlayer.start();
                }
            });
            lay.removeView(Rel);
            lay.addView(layout);
        }else if(Res.endsWith(".gif")){
            layout = (LinearLayout) inflater.inflate(R.layout.gif_item, null);
            GifImageView gifImageView=(GifImageView) layout.findViewById(R.id.info_gif1_A);
            File saveImgPath = getImageDir(questionDetailed.this);
            File gifSavePath1 = new File(saveImgPath, "gif1");
            displayImage(Res, gifSavePath1, gifImageView);

            lay.removeView(Rel);
            lay.addView(layout);
        }else if(Res.equals(Urls.base)&&m3u8.endsWith(".m3u8")){
            layout = (LinearLayout) inflater.inflate(R.layout.saoku_video_item, null);
            ImageView Img=(ImageView)layout.findViewById(R.id.detail_img);
            if(Thumb.equals(Urls.base)){
                Img.setImageDrawable(getResources().getDrawable(R.mipmap.makefun));
            }else {
                Picasso.with(questionDetailed.this).load(Thumb).into(Img);
            }
            replaceVieo(m3u8,layout,Thumb);
            lay.removeView(Rel);
            lay.addView(layout);
        } else {
            lay.setVisibility(View.GONE);
        }
    }
    public void displayImage(String url, File saveFile, final GifImageView gifView){
        HttpUtils http = new HttpUtils();
        // 下载图片
        http.download(url, saveFile.getAbsolutePath(), new RequestCallBack<File>() {
            public void onSuccess(ResponseInfo<File> responseInfo) {
                try {
                    Log.e("tag", "onSuccess========" + responseInfo.result.getAbsolutePath());
                    GifDrawable gifFrom = new GifDrawable( responseInfo.result.getAbsolutePath() );
                    gifView.setImageDrawable(gifFrom);
                }
                catch(Exception e){
                    Log.e("tag", e.getMessage());
                }
            }

            @Override
            public void onFailure(com.lidroid.xutils.exception.HttpException e, String s) {
                Log.e("tag", "onFailure========" + s);
                }});
    }
    public File getFilesDir(Context context, String tag){
        if(isSdCardExist() == true){
            return context.getExternalFilesDir(tag);
        }
        else{
            return context.getFilesDir();
        }
    }
    public File getImageDir(Context context){
        File file = getFilesDir(context, "images");
        return file;
    }
    public boolean isSdCardExist() {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            return true;
        }
        return false;
    }

    @Override
    public void onError() {

    }
    @Override
    public void onExpend() {

    }
    @Override
    public void onShrik() {

    }
    //EditText的监听器
    private class TextChange implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            comment=editTextComment.getText().toString();
        }
    }
    private void shareArticleToWx(final int flag) {


        if (!api.isWXAppInstalled()) {
            Toast.makeText(questionDetailed.this, "未安装微信客户端，请先下载",
                    Toast.LENGTH_LONG).show();
            return;
        }
        RequestParams paramstoWX=new RequestParams();
        paramstoWX.put("problem_id",problem_id);
        RequestHandle handlWX=client.post(Share,paramstoWX,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                String jsonStr=response.toString();
                try {
                    JSONObject jsonobj=new JSONObject(jsonStr);
                    JSONObject data=jsonobj.getJSONObject("data");
                    url=data.getString("url");
                    WXtitle="     "+data.getString("title");
                    WXdescription=data.getString("description");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        WXWebpageObject webpage=new WXWebpageObject();
        webpage.webpageUrl=url;
        WXMediaMessage msg= new WXMediaMessage(webpage);

        msg.title=WXdescription+WXtitle;
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
        msg.setThumbImage(bitmap);
         SendMessageToWX.Req req=new SendMessageToWX.Req();
         req.transaction=String.valueOf(System.currentTimeMillis());
        req.message=msg;
        req.scene=flag;
        api.sendReq(req);

    }



    private void replaceVieo(final String path,LinearLayout layout,final String thumb){
        ImageView play=(ImageView)layout.findViewById(R.id.saoku_play_btn);
        final VideoPlayView playView=(VideoPlayView)layout.findViewById(R.id.saoku_video_play_view);
        final FrameLayout showLayout=(FrameLayout)layout.findViewById(R.id.saoku_show_layout);
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isPlaying = true;
                showLayout.setVisibility(View.GONE);
                playView.setUrl(path);
                playView.setThumb(thumb);
                setPlayView(playView);
                playView.openVideo();
                playView.setVisibility(View.VISIBLE);
            }
        });
    }
    public void setPlayView(VideoPlayView playView) {
        playView.setMediaPlayerListenr(this);
    }
    //传递特效参数
    public void saokuResource(String sp_typeA){
        log.e("onSuccess",sp_typeA);
        if(!sp_typeA.isEmpty()){

            Bundle bundle = new Bundle();
            Intent intent=new Intent();
            bundle.putString("special",sp_typeA);
            intent.putExtras(bundle);
            intent.setClass(questionDetailed.this,viewSpecial.class);
            startActivity(intent);
        }
    }
}

